﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarLoanSystem
{
    public partial class panelReportDebtInfo : UserControl
    {
        public panelReportDebtInfo()
        {
            InitializeComponent();
            genGridview();
        }

        private void genGridview()
        {
            // gen Grideview
            gvDebt.AutoGenerateColumns = false;

            //Set Columns Count
            gvDebt.ColumnCount = 6;

            //Add Columns
            gvDebt.Columns[0].Name = "EMPLOYEE_ID";
            gvDebt.Columns[0].HeaderText = "รหัสพนักงาน";
            gvDebt.Columns[0].DataPropertyName = "EMPLOYEE_ID";
            gvDebt.Columns[0].Width = 95;

            gvDebt.Columns[1].Name = "EMPLOYEE_NAME";
            gvDebt.Columns[1].HeaderText = "ชื่อ";
            gvDebt.Columns[1].DataPropertyName = "EMPLOYEE_NAME";
            gvDebt.Columns[1].Width = 90;

            gvDebt.Columns[2].Name = "EMPLOYEE_LASTNAME";
            gvDebt.Columns[2].HeaderText = "นามสกุล";
            gvDebt.Columns[2].DataPropertyName = "EMPLOYEE_LASTNAME";
            gvDebt.Columns[2].Width = 90;

            gvDebt.Columns[3].Name = "WORK_ALL_AMOUNT";
            gvDebt.Columns[3].HeaderText = "จำนวนงานที่ได้รับมอบหมาย";
            gvDebt.Columns[3].DataPropertyName = "WORK_ALL_AMOUNT";
            gvDebt.Columns[3].Width = 160;

            gvDebt.Columns[4].Name = "WORK_AMOUNT";
            gvDebt.Columns[4].HeaderText = "จำนวนหนี้ที่กำลังดำเนินการ";
            gvDebt.Columns[4].DataPropertyName = "WORK_AMOUNT";
            gvDebt.Columns[4].Width = 155;

            gvDebt.Columns[5].Name = "WORK_AMOUNT_COMPLETE";
            gvDebt.Columns[5].HeaderText = "จำนวนงานที่ทวงหนี้คืนได้";
            gvDebt.Columns[5].DataPropertyName = "WORK_AMOUNT_COMPLETE";
            gvDebt.Columns[5].Width = 160;

            // end gen gridview
            //gvDebt.DataSource = LoadReportDept();
        }

        private DataTable LoadReportDept()
        {
            DataTable dt = DAL.ReportInfoDAL.LoadAllDept(this.txtEmpId.Text, this.txtName.Text, this.txtLastName.Text);
            if (dt != null)
            {
                dt.Columns.Add("WORK_AMOUNT", typeof(string));
                dt.Columns.Add("WORK_AMOUNT_COMPLETE", typeof(string));

                foreach (DataRow dr in dt.Rows)
                {
                    if (dr[4].ToString().Equals(""))
                    {
                        dr[6] = "0";
                    }
                    else
                    {
                        dr[6] = dr[4].ToString();
                    }

                    if (dr[5].ToString().Equals(""))
                    {
                        dr[7] = "0";
                    }
                    else
                    {
                        dr[7] = dr[5].ToString();
                    }
                }
            }

            return dt;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvDebt.DataSource = LoadReportDept();
        }

        private void btnRpt_Click(object sender, EventArgs e)
        {

            ShowReportDept s = new ShowReportDept(LoadReportDept());
            s.ShowDialog();
        }
    }
}

